import { nanoid } from 'nanoid';
import axios from 'axios';
const Person = {
  namespaced: true,
  state: () => ({
    personList: [],
  }),
  mutations: {
    add(state, value) {
      if (value.trim() == null || value == null || value == '') {
        alert('输入的值为空');
        return;
      }
      let objPerson = {
        id: nanoid(),
        name: value,
      };
      state.personList.unshift(objPerson);
    },
    addWang(state, value) {
      console.log(value.toString());
      if (value.substring(0, 1) == '王') {
        console.log(state);
        state.personList.unshift(value);
      } else {
        alert('第一个姓名不是王');
      }
    },
    addPersonServer(state, value) {
      let objPerson = {
        id: nanoid(),
        name: value,
      };
      state.personList.unshift(objPerson);
    },
  },
  actions: {
    addPersonServer(context) {
      axios
        .get('https://api.uixsj.cn/hitokoto/get?type=social')
        .then((res) => {
          context.commit('addPersonServer', res.data);
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
  getters: {},
};
export default Person;
